package com.ai.service;

import com.ai.entity.QjcTextSplitter;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DocumentService {

    @Value("classpath:meituan-qa.txt") // This is the text document to load
    private Resource resource;

    private final VectorStore vectorStore;

    public void add(String content) {
        List<Document> documents = new ArrayList<>();
        documents.add(new Document(content));

        QjcTextSplitter qjcTextSplitter = new QjcTextSplitter();
        List<Document> list = qjcTextSplitter.apply(documents);

        // 向量存储
        vectorStore.add(list);
    }

    public List<Document> loadText() {
        TextReader textReader = new TextReader(resource);
        textReader.getCustomMetadata().put("filename", "meituan-qa.txt");
        List<Document> documents = textReader.get();

        QjcTextSplitter qjcTextSplitter = new QjcTextSplitter();
        List<Document> list = qjcTextSplitter.apply(documents);

        // 向量存储
        vectorStore.add(list);

        return list;
    }
}
